import {
	WARN_LEVEL_MAP,
	WARNING_DEDUCTION_RULE_MAP,
	TASK_CATEGORY_TYPES_MAP,
} from '../../common/constants';

export default function calculateScore(job) {
	const weaknessWarnings = TASK_CATEGORY_TYPES_MAP.weakness
		.reduce((arr, taskType) => {
			return arr.concat(job[`${taskType}Result`]?.result?.warning);
		}, [])
		.filter(w => !w?.isResolved && !w?.ignoredTime);

	const securityEventWarnings = TASK_CATEGORY_TYPES_MAP.securityEvent
		.reduce((arr, taskType) => {
			return arr.concat(job[`${taskType}Result`]?.result?.warning);
		}, [])
		.filter(w => !w?.isResolved && !w?.ignoredTime);

	const weaknessRate = weaknessWarnings.reduce((res, w) => {
		res -= WARNING_DEDUCTION_RULE_MAP[WARN_LEVEL_MAP[w.level]];
		return res > 0 ? res : 0;
	}, 50);

	const securityEventRate = securityEventWarnings.reduce((res, w) => {
		res -= WARNING_DEDUCTION_RULE_MAP[WARN_LEVEL_MAP[w.level]];
		return res > 0 ? res : 0;
	}, 50);

	return weaknessRate + securityEventRate;
}
